{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-10T06:28:14.572692Z",
     "start_time": "2025-10-10T06:28:14.559255Z"
    }
   },
   "source": [
    "# 导入 f1_score 函数用于计算 F1 分数\n",
    "from sklearn.metrics import f1_score"
   ],
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-10T06:31:16.477510Z",
     "start_time": "2025-10-10T06:31:16.454094Z"
    }
   },
   "cell_type": "code",
   "source": [
    "true_labels = [1, 1, 1, 0, 1, 1]\n",
    "predicted_labels = [1, 1, 1, 1, 1, 1]\n",
    "# 计算二分类的 F1 分数（binary 平均方式）\n",
    "f1_binary = f1_score(true_labels, predicted_labels, average='binary')\n",
    "print(\"%.2f\" % f1_binary)\n",
    "true_labels_2 = [0, 1, 2, 0, 1, 2]\n",
    "predicted_labels_2 = [0, 2, 1, 1, 1, 1]\n",
    "# 计算多分类的 F1 分数（micro 平均方式）\n",
    "f1_micro = f1_score(true_labels_2, predicted_labels_2, average='micro')\n",
    "print(\"%.2f\" % f1_micro)"
   ],
   "id": "5a4e9101d535a9dc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.91\n",
      "0.33\n"
     ]
    }
   ],
   "execution_count": 7
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
